/**
* name 
*/
namespace ghost{
	export class LevelConfig{
		public static monsters:any;
		public static bosses:any;
		public static buffs:any;
		public static reduceBuffs:any;
		public static pos:number[]=[20816,19592,18368,17144,15920,14696,13472,12248,11024,9800,8576,7352,6128,4904,3680,2456,632];		
		public static hps:number[]=[40,60,117,185,244,297,359,412,475,531,588,654,708,765,831,890,948,1006];		
		public static randomHp():void
		{
			var i,len;len=18;
			var str="";
			for(i=0;i<len;++i){str+=(i*(1000/17)+(Math.random()*10)|0)+","};
			console.log(str);
			
		}
		public static randomMPos():void
		{
			var i,len;
			len=18;
			var str="";
			for(i=0;i<len;++i){str+=(((22040)-i*(22040/len|0))+",")};
			console.log(str);
		}
		public static randomPos():void
		{
			var i,len;
			len=18;
			var str="";
			for(i=0;i<len;++i){str+=(((22040)-i*(22040/len|0)-700)+",")};
			console.log(str);
		}
		/**随机打乱怪物
		 * @param len 截取数组长度
		 * @param count 最大数量
		*/
		public static randomIndexes(len:number,count:number=NaN):number[]
		{
			let i:number;
			let arr:number[] = [];
			let ind:number,t:number;
			if(count != count)
				count = len;
			for(i=0;i<len;++i)			
				arr[i] = i;
			for(i=0;i<len-1;++i)
			{
				ind = i + (Math.random()*(len - i - 1)|0) + 1;				
				t = arr[i];
				if(arr[ind] == null)
				debugger;
				arr[i] = arr[ind];
				arr[ind] = t;
			}			
			if(count > arr.length)
				arr = LevelConfig.randomIndexes(len,count - arr.length).concat(arr);
			else
				arr.length = count;
			return arr;
		}
		public static output(level:number=1):string
		{			
			let bulletHurt:number,power:number,emitSpd:number;
			bulletHurt = 4;
			power = 1;
			emitSpd = 1.5;
			let str:string = "";			
			if(LevelConfig.monsters == null)
			{
				LevelConfig.monsters = [
					{
						"roleID": "101"
					},{
						"roleID": "102"
					},{
						"roleID": "103"
					},{
						"roleID": "104"
					},{
						"roleID": "105"
					},{
						"roleID": "106"
					},{
						"roleID": "107"
					},{
						"roleID": "108"
					},{
						"roleID": "109"
					},{
						"roleID": "110"
					}
				];
			}
			if(LevelConfig.bosses == null)
			{
				LevelConfig.bosses = [
					{
						"roleID": "111"
					},{
						"roleID": "112"
					},{
						"roleID": "113"
					},{
						"roleID": "114"
					},{
						"roleID": "115"
					}
				];
			}
			
			if(LevelConfig.buffs == null)
			{
				LevelConfig.buffs = [
					{
						roleID: "300001",
						add_trajectory:1
					},{
						roleID: "300002",
						up_power:-1,
					},{
						roleID: "300003",						
						up_power: 1
					},{
						roleID: "300004",
						up_attackSpeed:-1
					},{
						roleID: "300005",						
						up_attackSpeed: 1
					}
				];	
				
			}
			let reduceBuff:any;
			let i:number,len:number;
			let arr:any[],buffArr:any[];
			let cfg:any = {
				"sceneInfo": {
					"mapName": "A",
					"backScaleX": 0.85,
					"backScaleY": 0.25,
					"mapAreaSize": 485,
					"maxAreaX": 2,
					"maxAreaY": 1,
					"minBorderX":120,
					"maxBorderX":600,			
					"mapMaxX": 720,
					"mapMaxY": 23040
				},
				"areaRoles": null,
				"sceneRoles": {},
				"screenMonster": [],		
				"screenObjects": [],
				"myPlayer": {
					"id": "x",
					"roleID": "1",
					"roleType": 0,
					"roleListType": 0,
					"trajectorys":["100001"],
					"power":1,
					"attackSpeed":1.5,
					"trajectoryLimit":3,
					"smart": "autoShoot",
					"skin": {
						"skinType": 2,
						"clothes": "1",
						"action": "1"
					}
				},
				"upPowerLimitY":400,
				"playerXRange":240,
				"playerLRSpeed":120,
				"playerMoveSpeed": 4,
				"playerInitX": 360,
				"playerInitY": 23000,
				"atalsWidth": 720,
				"atalsHeight": 485,
				"bulletPower":4
			};			
			let obj:any;
			let randomIndexes:number[],buffRandomIndexes:number[];
			let num:number,monsterTypes:number;
			let l:number,m:number,r:number;
			let bossIndex:number,bossAdd:number,monsterAdd:number;
			let rnd:number,hurt:number,lastPos:number,c:number;
			let monsterInd:number,buffInd:number;
			let monsterHp:number = 0;
			let hardest:number = 1.2;//难度，越大越难
			let defaultBuffPower:number = 1;
			let levelAdd:number,trajectoryAdd:number,powerAdd:number;
			let petBuff:any;
			let lastMonsterPos:number,maxSec:number,minSec:number;
			let posModes:number[][];
			let monsterRand:number,buffRand:number,addBuffRand:number,reduceBuffRand:number;
			let buffRandInd:number;			
			monsterRand = 0.5;
			buffRand = 0.1;
			buffRandInd = 0;
			// addBuffRand = 0.3;
			// reduceBuffRand = 0.1;			
			levelAdd = 1.5 - 30/(59+level);//Math.pow(level,1/5);
			trajectoryAdd = 1;
			hurt=100;
			l = 210;
			m = 360;
			r = 510;
			posModes = [[l,r],[m],[l],[r]];
			bossAdd = 4;
			monsterAdd = 10;
			powerAdd = 0;
			
			if(level < 11)//A级
			{
				monsterTypes = 10;
				bossIndex = level%5;
				cfg.sceneInfo.mapAreaSize = 486;
				cfg.atalsHeight = 486;
				cfg.sceneInfo.mapName = "A";		
				minSec = 50;		
				maxSec = 90;
			}				
			else if(level < 21)//B级
			{
				monsterTypes = 10;
				bossIndex = level%5;
				cfg.sceneInfo.mapAreaSize = 486;
				cfg.atalsHeight = 486;
				cfg.sceneInfo.mapName = "B";				
				minSec = 70;
				maxSec = 120;
			}				
			else //C级
			{
				monsterTypes = 10;
				bossIndex = level%5;
				cfg.sceneInfo.mapAreaSize = 486;
				cfg.atalsHeight = 486;
				cfg.sceneInfo.mapName = "C";				
				minSec = 100;
				maxSec = 150;				
			}				
			let levelIndex:number;
			let addHurt:number;			
			levelIndex = (level - 1) % 10;
			cfg.sceneInfo.mapMaxY = 23040 / 90 * Math.min(maxSec,(minSec + 5 * levelIndex))|0;
			cfg.playerInitY = cfg.sceneInfo.mapMaxY - 40;
			num = cfg.sceneInfo.mapMaxY/ 1000|0;
			randomIndexes = LevelConfig.randomIndexes(monsterTypes,num);
			buffRandomIndexes = LevelConfig.randomIndexes(LevelConfig.buffs.length,num);
			arr = LevelConfig.monsters;
			buffArr = LevelConfig.buffs;
			len = i = (cfg.sceneInfo.mapMaxY/100|0)*100;
			lastPos = -1;
			c = 0;
			
			petBuff = {
						roleID: "300006"
					};
			let buffCount:number=num/2|0;			
			for(buffInd=0,monsterInd = 0;i>0;i-=100)
			{				
				addHurt = (len/(i+len)-0.5)*hurt|0;
				if(i == len - 1400)
				{
					//先放置入魔，修仙
					obj ={
						"roleID": "300007",						
						"up_skinObj": {
							"skinType": 2,
							"clothes": "2",
							"action": "2"							
						},
						buffIndex:9999
					}
					obj.absX = l;
					obj.absY = i;	
					cfg.screenObjects.unshift(obj);
					obj = {
						"roleID": "300008",						
						"up_skinObj": {
							"skinType": 2,
							"clothes": "3",
							"action": "3"							
						},
						buffIndex:9999
					}
					obj.absX = r;
					obj.absY = i;	
					cfg.screenObjects.unshift(obj);		
					lastPos = i;
					
				}
				else if(lastPos != -1)
				{
					if(i == 400)//最终boss
					{
						obj = JSON.parse(JSON.stringify(LevelConfig.bosses[bossIndex]));
						obj.absX = m;
						obj.absY = i;
						obj.hp = hurt*hardest + addHurt*bossAdd|0;
						// obj.playerMinPower = monsterHp/4000+power >> 1;
						// obj.playerMinEmitSpd = emitSpd;
						// obj.playerMinTrajectoryNum = Math.min(3,trajectoryAdd);
						obj.monsterType = 1;
						cfg.screenMonster.unshift(obj);
						++monsterInd;							
						break;
					}
					if(lastPos - i > 500 && i > 2500)
					{					
						let posArr:number[];
						let posInd:number,posLen:number;
						let pos:number;	
						let y:number;
						let hasBuff:boolean,hasMonster:boolean;
						r = Math.random();
						if(r < 0.6)
							posArr = posModes[0];
						else if(r < 0.8)
							posArr = posModes[1];
						else if(r < 0.9)
							posArr = posModes[2];
						else
							posArr = posModes[2];
						y = i - Math.random()*100|0;
						hasBuff = false;
						hasMonster = false;						
						posLen = posArr.length;						
						while(--posLen > -1)
						{
							pos = posArr[posLen];							
							r = Math.random();
							if(r > monsterRand && hasMonster)
								r = Math.random() * monsterRand;
							if(r > monsterRand)
							{//创建怪物										
								hasMonster = true;												
								obj = JSON.parse(JSON.stringify(arr[randomIndexes[monsterInd%randomIndexes.length]]));														
								// while(1)
								// {
								// 	rnd = Math.random();
								// 	pos = rnd > 0.66?r:(rnd > 0.33?m:l);
								// 	if(pos != lastMonsterPos)
								// 		break;
								// }
								lastMonsterPos = pos;
								obj.absX = pos;
								obj.absY = y;
								obj.hp = hurt*hardest + monsterAdd * addHurt  * Math.random()|0;
								obj.score = 1 + ((len - i)/1000|0);
								monsterHp += obj.hp;
								cfg.screenMonster.unshift(obj);
								lastPos = i;
								++c;
								++monsterInd;
								// //随机产生双怪拦路
								// if(Math.random() > 0.2 && (rnd < 0.33 || rnd > 0.66))
								// {
								// 	let otherObj:any;
								// 	let mRndInd:number;
								// 	mRndInd = randomIndexes[randomIndexes.length*Math.random()|0];
								// 	otherObj = JSON.parse(JSON.stringify(arr[mRndInd]));
								// 	otherObj.absX = rnd > 0.66?l:r;
								// 	otherObj.absY = obj.absY - (200 * Math.random() - 200)|0;
								// 	otherObj.hp = obj.hp * (1+Math.random() * 0.3)|0;
								// 	cfg.screenMonster.unshift(otherObj);																								
								// 	monsterHp += obj.hp*0.5;
								// }	
								// //随机产生三怪拦路
								// if(Math.random() > 0.5 && (rnd < 0.33 || rnd > 0.66))
								// {
								// 	let otherObj:any;
								// 	let mRndInd:number;
								// 	mRndInd = randomIndexes[randomIndexes.length*Math.random()|0];
								// 	otherObj = JSON.parse(JSON.stringify(arr[mRndInd]));
								// 	otherObj.absX = m;
								// 	otherObj.absY = obj.absY + 600 * Math.random() |0;
								// 	otherObj.hp = obj.hp * 0.5|0;
								// 	cfg.screenMonster.unshift(otherObj);
								// 	monsterHp += obj.hp;
								// }																			
								monsterRand = (monsterRand + 1) / 2;
								power+=1;
							}
							else if(r > buffRand)
							{//创建buff
								lastPos = i;
								++c;									
								--buffCount;									
								monsterRand = (monsterRand + buffRand) / 2;
								//允许随机跳过n次buff，保证最低也有7个buff
								// const max:number= 7;
								// if(Math.random() > ((max-buffInd)/(buffCount+1) + 0.5))
								// {									
								// 	continue;
								// }
								hasBuff = true;									
								// if(r > addBuffRand)
								// {
									// rnd = Math.random();									
									if(petBuff && i < len * 0.75)
									{
										// petBuff.absX = rnd > 0.5?r:l;
										petBuff.absX = pos;
										petBuff.absY = y;
										cfg.screenObjects.unshift(petBuff);		
										petBuff.buffIndex = buffInd;						
										petBuff = null;
										bulletHurt += defaultBuffPower*levelAdd;
									}
									else
									{
										obj = JSON.parse(JSON.stringify(buffArr[buffRandomIndexes[buffRandInd%buffRandomIndexes.length]]));
										// obj.absX = rnd > 0.5?r:l;
										obj.absX = pos;
										obj.absY = y;
										obj.buffIndex = buffInd;
										cfg.screenObjects.unshift(obj);		
										++buffRandInd;											
									}		
									if(obj && obj.roleID != "300002" &&  obj.roleID != "300004")
									{
										if(obj.up_power)
										{								
											obj.up_power = defaultBuffPower * levelAdd;								
											power += obj.up_power;
										}								
										if(obj.add_trajectory)
										{
											if(trajectoryAdd <= 3)
											{
												trajectoryAdd += obj.add_trajectory;											
												bulletHurt += trajectoryAdd*levelAdd;
											}											
										}
										if(obj.up_attackSpeed)
										{
											emitSpd += obj.up_attackSpeed*levelAdd;		
											if(emitSpd > 10)
												emitSpd = 10;
										}											

										//初始值 移速=4 发射速度=10 力量=1，子弹伤害=4，一屏射击180hp
										//伤害 = (4/移速)*(力量)*(子弹伤害/4)*(发射速度/10)*180 = (力量)*(子弹伤害)*(发射速度)*18/移速			
										hurt = power*bulletHurt*emitSpd*18/4;											
									}	
																		
								// }											
								// else
								// {
								// 	reduceBuff = Math.random()>0.5?{
								// 		roleID: "300002",
								// 		up_power:-1,
								// 	}:{
								// 		roleID: "300004",
								// 		up_attackSpeed:-1
								// 	};
																
								// 	reduceBuff.absX = pos;
								// 	reduceBuff.absY = y;	
								// 	reduceBuff.buffIndex = buffInd;
								// 	cfg.screenObjects.unshift(reduceBuff);									
								// }								
							}
						}
						if(hasBuff)
						{
							++buffInd;
						}
							
					}								
					
				}
			}
			cfg.monsterNum = cfg.screenMonster.length;
			cfg.buffNum = cfg.screenObjects.length;
			cfg = {sceneConfig:cfg};
			return cfg;
		}
		public static downloadConfigs(map:string[],level:number[]):void
		{
			let i:number,len:number;
			if(level)
			{
				if(map == null || map.length == 0)
					map = ["A"];
				len= level.length;
				for(i=0;i<len;++i)
				{
					LevelConfig.downloadConfig(map[i]?map[i]:map[0],level[i]);
				}
			}			
		}
		public static downloadConfig(map:string,level:number):void
		{
			LevelConfig.download(map+"_"+((level-1)%10 + 1)+".json",JSON.stringify(ghost.LevelConfig.output(level),null,"	"));
		}
		/**下载文件 ,调用方法 download("save.txt","内容");
		 * @param obj 抛出事件的dom对象
		 * @param data ArrayBuffer 或者 Blob
		 * @param type mimeType {type:"image/png"}等
		*/
		public static download(name, data, type:any = null) {
			var urlObject:any = window.URL || window["webkitURL"] || window;

			var downloadData = new Blob([data],type);

			var save_link:any = document.createElementNS("http://www.w3.org/1999/xhtml", "a")
			save_link.href = urlObject.createObjectURL(downloadData);
			save_link.download = name;
			LevelConfig.dispatch_click(save_link);
		}
		public static dispatch_click(obj) {
			var ev = document.createEvent("MouseEvents");
			ev.initMouseEvent(
				"click", true, false, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null
			);
			obj.dispatchEvent(ev);
		}
	}
}
//ghost.LevelConfig.downloadConfigs(["A","A","A","A","A","A","A","A","A","A"],[1,2,3,4,5,6,7,8,9,10]);
//ghost.LevelConfig.downloadConfigs(["C","C","C","C","C","C","C","C","C","C"],[21,22,23,24,25,26,27,28,29,30]);
//ghost.LevelConfig.downloadConfigs(["B","B","B","B","B","B","B","B","B","B"],[11,12,13,14,15,16,17,18,19,20]);